//-------helper-------
function str2int(x){
    return x.map(function (i) { return parseInt(i, 10) })
}
var SUM = function (x) { var sum = 0; for (var i in x) sum += x[i]; return sum; }
var AVG = function (x) { return SUM(x) / x.length; }
var VAR = function (x) { var tmp = 0; var xavg = AVG(x); for (var i in x) tmp += (x[i] - xavg) * (x[i] - xavg); return tmp / (x.length - 1); }

function nextInt(n) {
    return Math.round(Math.random() * n)
}

function shuffle(arr) {
    for (var j, x, i = arr.length; i; j = parseInt(Math.random() * i), x = arr[--i], arr[i] = arr[j], arr[j] = x);
    return arr;
};

function parseData(v) {
    var i;
    var r = []
    var tmp = {}
    for (i in v) {
        var score = v[i]
        if (score in tmp) {
            tmp[score] = tmp[score] + 1;
        } else {
            tmp[score] = 1;
        }
    }
    for (var i = 0; i < 7; i++) {
        if (i in tmp) {
            r.push(tmp[i])
        } else {
            r.push(0)
        }
    }
    return r;
}

function count2frequency(countList) {
    countList = parseData(countList)
    var sum = 0
    for (var i in countList) {
        sum += countList[i]
    }
    var r = []
    for (var i in countList) {
        r.push(countList[i] / sum)
    }
    return r
}


// ------ main -------
function tTest(x, y) {
    // VAR:样本方差的无偏估计，注意不是标准差！
    // 详见：https://en.wikipedia.org/wiki/Student%27s_t-test
    //       https://en.wikipedia.org/wiki/Welch%27s_t_test
    //       http://stats.stackexchange.com/q/8029
    // 下表为当p取X时t统计量取Y的值。其中p为单侧估计的值。双侧需除2
    var tTable = {
        "18": {
            't': [0.688, 0.862, 1.067, 1.33, 1.734, 2.101, 2.552, 2.878, 3.197, 3.61, 3.922],
            'p': [0.25, 0.2, 0.15, 0.1, 0.05, 0.025, 0.01, 0.005, 0.0025, 0.001, 0.0005]
        },
        "19": {
            't': [0.688, 0.861, 1.066, 1.328, 1.729, 2.093, 2.539, 2.861, 3.174, 3.579, 3.883],
            'p': [0.25, 0.2, 0.15, 0.1, 0.05, 0.025, 0.01, 0.005, 0.0025, 0.001, 0.0005]
        },
        "20": {
            't': [0.687, 0.86, 1.064, 1.325, 1.725, 2.086, 2.528, 2.845, 3.153, 3.552, 3.85],
            'p': [0.25, 0.2, 0.15, 0.1, 0.05, 0.025, 0.01, 0.005, 0.0025, 0.001, 0.0005]
        },
        "21": {
            't': [0.686, 0.859, 1.063, 1.323, 1.721, 2.08, 2.518, 2.831, 3.135, 3.527, 3.819],
            'p': [0.25, 0.2, 0.15, 0.1, 0.05, 0.025, 0.01, 0.005, 0.0025, 0.001, 0.0005]
        },
        "22": {
            't': [0.686, 0.858, 1.061, 1.321, 1.717, 2.074, 2.508, 2.819, 3.119, 3.505, 3.792],
            'p': [0.25, 0.2, 0.15, 0.1, 0.05, 0.025, 0.01, 0.005, 0.0025, 0.001, 0.0005]
        },
        "23": {
            't': [0.127, 0.256, 0.390, 0.532, 0.685, 0.858, 1.060, 1.319, 1.714, 2.069, 2.500, 2.807, 3.104, 3.485, 3.767],
            'p': [0.45, 0.4, 0.35, 0.3, 0.25, 0.2, 0.15, 0.1, 0.05, 0.025, 0.01, 0.005, 0.0025, 0.001, 0.0005]
        },
        "24": {
            't': [0.685, 0.857, 1.059, 1.318, 1.711, 2.064, 2.492, 2.797, 3.091, 3.467, 3.745],
            'p': [0.25, 0.2, 0.15, 0.1, 0.05, 0.025, 0.01, 0.005, 0.0025, 0.001, 0.0005]
        },
        "25": {
            't': [0.684, 0.856, 1.058, 1.316, 1.708, 2.06, 2.485, 2.787, 3.078, 3.45, 3.725],
            'p': [0.25, 0.2, 0.15, 0.1, 0.05, 0.025, 0.01, 0.005, 0.0025, 0.001, 0.0005]
        },
        "26": {
            't': [0.684, 0.856, 1.058, 1.315, 1.706, 2.056, 2.479, 2.779, 3.067, 3.435, 3.707],
            'p': [0.25, 0.2, 0.15, 0.1, 0.05, 0.025, 0.01, 0.005, 0.0025, 0.001, 0.0005]
        },
        "27": {
            't': [0.684, 0.855, 1.057, 1.314, 1.703, 2.052, 2.473, 2.771, 3.057, 3.421, 3.69],
            'p': [0.25, 0.2, 0.15, 0.1, 0.05, 0.025, 0.01, 0.005, 0.0025, 0.001, 0.0005]
        },
        "28": {
            't': [0.127, 0.256, 0.389, 0.530, 0.683, 0.855, 1.056, 1.313, 1.701, 2.048, 2.467, 2.763, 3.047, 3.408],
            'p': [0.45, 0.4, 0.35, 0.3, 0.25, 0.2, 0.15, 0.1, 0.05, 0.025, 0.01, 0.005, 0.0025, 0.001]
        },
        "29": {
            't': [0.683, 0.854, 1.055, 1.311, 1.699, 2.045, 2.462, 2.756, 3.038, 3.396, 3.659],
            'p': [0.25, 0.2, 0.15, 0.1, 0.05, 0.025, 0.01, 0.005, 0.0025, 0.001, 0.0005]
        },
        "30": {
            't': [0.683, 0.854, 1.055, 1.31, 1.697, 2.042, 2.457, 2.75, 3.03, 3.385, 3.646],
            'p': [0.25, 0.2, 0.15, 0.1, 0.05, 0.025, 0.01, 0.005, 0.0025, 0.001, 0.0005]
        },
        "31": {
            't': [0.127, 0.256, 0.389, 0.530, 0.682, 0.853, 1.054, 1.309, 1.696, 2.040, 2.453, 2.744],
            'p': [0.45, 0.4, 0.35, 0.3, 0.25, 0.2, 0.15, 0.1, 0.05, 0.025, 0.01, 0.005]
        },
        "32": {
            't': [0.127, 0.255, 0.389, 0.530, 0.682, 0.853, 1.054, 1.309, 1.694, 2.037, 2.449, 2.738],
            'p': [0.45, 0.4, 0.35, 0.3, 0.25, 0.2, 0.15, 0.1, 0.05, 0.025, 0.01, 0.005]
        },
        "33": {
            't': [0.127, 0.255, 0.389, 0.530, 0.682, 0.853, 1.053, 1.308, 1.692, 2.035, 2.445, 2.733],
            'p': [0.45, 0.4, 0.35, 0.3, 0.25, 0.2, 0.15, 0.1, 0.05, 0.025, 0.01, 0.005]
        },
        "34": {
            't': [0.127, 0.255, 0.389, 0.529, 0.682, 0.852, 1.052, 1.307, 1.691, 2.032, 2.441, 2.728],
            'p': [0.45, 0.4, 0.35, 0.3, 0.25, 0.2, 0.15, 0.1, 0.05, 0.025, 0.01, 0.005]
        },
        "35": {
            't': [0.127, 0.255, 0.388, 0.529, 0.682, 0.852, 1.052, 1.306, 1.690, 2.030, 2.438, 2.724],
            'p': [0.45, 0.4, 0.35, 0.3, 0.25, 0.2, 0.15, 0.1, 0.05, 0.025, 0.01, 0.005]
        },
        "36": {
            't': [0.127, 0.255, 0.388, 0.529, 0.681, 0.852, 1.052, 1.306, 1.688, 2.028, 2.434, 2.719],
            'p': [0.45, 0.4, 0.35, 0.3, 0.25, 0.2, 0.15, 0.1, 0.05, 0.025, 0.01, 0.005]
        },
        "37": {
            't': [0.127, 0.255, 0.388, 0.529, 0.681, 0.851, 1.051, 1.305, 1.687, 2.026, 2.431, 2.715],
            'p': [0.45, 0.4, 0.35, 0.3, 0.25, 0.2, 0.15, 0.1, 0.05, 0.025, 0.01, 0.005]
        },
        "38": {
            't': [0.681, 1.304, 1.686, 2.024, 2.429, 2.712, 2.98, 3.319, 3.566],
            'p': [0.25, 0.1, 0.05, 0.025, 0.01, 0.005, 0.0025, 0.001, 0.0005]
        }
    }
    var tTableDf = function (tValue, df) {
        var tlist = tTable[df]['t']
        var plist = tTable[df]['p']
        for (var i = 1; i < tlist.length; i++) {
            if (tlist[i] >= tValue) {
                if (i == 1) {
                    return ['≥', plist[0]];
                } else {
                    return ['≤', plist[i - 1]]
                }
            }
        }
        return ['≤', plist[plist.length - 1]]
    }
    //str to int
    var x = str2int(x)
    var y = str2int(y)
    var n1 = x.length
    var n2 = y.length
    var s1 = VAR(x)
    var s2 = VAR(y)
    var avgX = AVG(x)
    var avgY = AVG(y)
    var meanDiff = avgX - avgY
    var t = (meanDiff) / Math.sqrt(s1 / n1 + s2 / n2)
    var df = Math.round(Math.pow((s1 / n1 + s2 / n2), 2) / (Math.pow(s1 / n1, 2) / (n1 - 1) + Math.pow(s2 / n2, 2) / (n2 - 1)))
    var p = tTableDf(Math.abs(t), df + '');
    //console.log(t,df,p);
    return [ p , df , meanDiff.toFixed(3), avgX.toFixed(3), avgY.toFixed(3) ]
}


function adjustP(x,y,n){  // assume avg x > avg y
    x = str2int(x)
    y = str2int(y)
    var xLargeThanY = 0
    var xLen = x.length
    var yLen = y.length
    var total = x.concat(y)
    var xAvg = AVG(x)
    var yAvg = AVG(y)
    for(var i=0; i<n; i++){
        shuffle(total)
        var sumX=0,sumY=0
        for(var j=0;j<xLen;j++){
            sumX += total[j]
        }
        for (var j = xLen; j < xLen+yLen; j++){
            sumY += total[j]
        }
        var avgX = sumX/xLen
        var avgY = sumY/yLen
        if (xAvg - yAvg > avgX - avgY){
            xLargeThanY++;
        }
    }
    return (n-xLargeThanY)/n
}

module.exports = {
    tTest: tTest,
    count2frequency: count2frequency,
    adjustP: adjustP,
    str2int: str2int,
}